import { Request } from "../../../utils/request.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 
   * @param {上传照片接口} param 
   */
  _imgupload: function (param) {
    let url = wx.getStorageSync("config");
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        const uploadTask = wx.uploadFile({
          // url: 'https://zhiyu.zyfuns.com/api/Zyupdateuserinfo/UpdateUserAvatar', //仅为示例，非真实的接口地址
          url: url.service.avatarImgUrl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            //苹果手机不能使用 ?<=
            // this.upImgesUrl = JSON.parse(res.data).msg.match(/(?<=public).+/i)[0];  //图片存储路径，直接存入数据库中
            this.upImgesUrl = JSON.parse(res.data).upImgesUrl;
            this.setData({
              //头像展示路径
              userimage2: url.service.host + this.upImgesUrl,
              //图片路径
              userimage: this.upImgesUrl,
            });

          }.bind(this),
        });
        uploadTask.onProgressUpdate(function (res) { //监听文件上传
          new Request().showToast(`加载中...${res.progress}%`);
        });
      }.bind(this),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('account', (data) => {
      console.log(data)
      this.account_info = data;
    });
    let that = this;
    //基本信息标题接口
    new Request('basic/Get_Register_Basic/GetList').request(
      () => {
        return {
          openid: app.globalData.openid,
        }
      },
      (res) => {
        res.data.data.map((e) => {
          e.self_id = 0;
          e.want_id = 0;
          e.self_text = '未选择';
          e.want_text = '不限';
          //默认是单一满足 1是单一满足 2是全部满足
          e.match_status = 1;
        });
        that.setData({
          basic_list: res.data.data,
        });
      }
    );
  },
  /**
   * 保存按钮 上传信息
   */
  save: function (param) {
    console.log(this.data.basic_list);
    let arr = this.data.basic_list;
    for (let i = 0, l = arr.length; i < l; i++) {
      if (arr[i].self_id == 0) {
        new Request().showToast(`我的${arr[i].root_name}未选择`);
        return;
      }
    }
    this.account_info['avatar'] = this.data.userimage;
    this.account_info['nichen'] = this.user_nichen;
    this.account_info['openid'] = app.globalData.openid;
    //判断是否有输入推荐人ID
    if (this.object_id != '' && this.object_id != undefined) {
      this.account_info['push_id'] = this.object_id;
    }
    if (this.data.userimage == "" || this.data.userimage == null || this.data.userimage == undefined) {
      new Request().showToast('请上传头像');
      return;
    }
    if (this.user_nichen == "" || this.user_nichen == null || this.user_nichen == undefined) {
      new Request().showToast('昵称未填写');
      return;
    }
    new Request('basic/Register_Basic/Register').request(
      () => {
        return {
          basis_list: this.data.basic_list,
          account_info: this.account_info
        }
      },
      (res) => {
        if (res.data.code == 1) {
          app.globalData.loginCode = 1;
          app.globalData.userid = res.data.data;
          new Request().showToast(res.data.msg, 'none', () => {
            setTimeout(() => {
              wx.switchTab({
                url: '../../../pages/zhaon_home/zhao_home'
              });
            }, 2000);
          });
          return;
        }
      },
      'application/json'
    );


    return;
  },
  /**
   * 
   * @param {跳转选项列表} param 
   */
  GoToChooseList: function (event) {
    //根属性id
    let root_id = event.currentTarget.dataset.rootid;
    //索引下标
    let index = event.currentTarget.dataset.index;
    //选项类型 1是单选 2是多选
    let type = event.currentTarget.dataset.listtype;
    //判断是否是地址选项列表
    // let area = ['定居地'];
    let areaType = ['定居地'].indexOf(this.data.basic_list[index].root_name);
    //跳转选项地址
    var url = '/pages/zhaon_register/zhaon_xuanzhe/zhaon_xuanzhe';


    let basic = {
      //1是普通选项，2是地址选项
      'choose_type': areaType != '-1' ? '2' : '1',
      //某一列索引
      'index': index,
      //1是单选列表，2是多选列表
      'list_type': type,
      //某一列数据
      'arr_data': this.data.basic_list[index],
    };
    wx.navigateTo({
      url: url,
      events: {
        back: (res) => {
          this.data.basic_list[res.index] = res.arr_data;
          this.setData({
            basic_list: this.data.basic_list
          });
          console.log(this.data.basic_list);
        }
      },
      success: (res) => {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('basic', basic);
      }
    });
  },
  /**
   * 昵称写入监听
   */
  emailInput: function (e) {
    this.user_nichen = e.detail.value;
  },
  emailInput2: function (e) {
    this.object_id = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})